package run.siyuan.jdk.尚硅谷算法.数组;

import java.io.*;

/**
 * {@code ClassName:} SparseArr
 * {@code Description:} TODO 稀疏数组
 * {@code Author:} yuan
 * {@code Date:} 2023/3/30 17:27
 */
public class SparseArrTest {

    static int row = 11;
    static int col = 11;
    static int[][] sourceArr = new int[row][col];

    public static void main(String[] args) {
        sourceArray();
        int[][] sparseArray = twoArrayToSparseArray();
        System.out.println("二维数组转稀疏数组：");
        showArr(sparseArray);
        int[][] twoArray = sparseArrayToTwoArray(sparseArray);
        System.out.println("稀疏数组转二维数组：");
        showArr(twoArray);

        System.out.println("文件中稀疏数组转二维数组：");
        showArr(sparseArrayToTwoArray());
    }


    /**
     * 源数组
     */
    public static void sourceArray() {
        sourceArr[1][2] = 1;
        sourceArr[2][3] = 2;
        sourceArr[4][5] = 2;
        System.out.println("源数组元素");
        showArr(sourceArr);
    }

    /**
     * TODO  二维数组转稀疏数组 实现压缩功能，减少存储多个相同的元素
     */
    public static int[][] twoArrayToSparseArray() {
        int sum = 0;
        for (int[] ints : sourceArr) {
            for (int anInt : ints) {
                if (anInt > 0) {
                    sum++;
                }
            }
        }
        int[][] sparseArr = new int[sum + 1][3];
        try {

            sparseArr[0][0] = row;
            sparseArr[0][1] = col;
            sparseArr[0][2] = sum;
            int count = 0;
            FileWriter fileWriter = new FileWriter("/Users/wuzhiqian/idea_repo/siyuan/doc/data.txt");
            BufferedWriter writer = new BufferedWriter(fileWriter);
            writer.write(row + " " + col + " " + sum);
            writer.newLine();
            for (int i = 0; i < sourceArr.length; i++) {
                int[] arr = sourceArr[i];
                for (int j = 0; j < arr.length; j++) {
                    if (arr[j] > 0) {
                        count++;
                        sparseArr[count][0] = i;
                        sparseArr[count][1] = j;
                        sparseArr[count][2] = arr[j];
                        writer.write(i + " " + j + " " + arr[j]);
                        writer.newLine();
                    }
                }
            }
            writer.close();
            fileWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        return sparseArr;
    }

    /**
     * 稀疏数组转二维数组
     *
     * @param sparseArr 稀疏数组
     */
    public static int[][] sparseArrayToTwoArray(int[][] sparseArr) {
        int row = sparseArr[0][0];
        int col = sparseArr[0][1];
        int ys;
        int[][] twoArray = new int[row][col];
        for (int i = 1; i < sparseArr.length; i++) {
            row = sparseArr[i][0];
            col = sparseArr[i][1];
            ys = sparseArr[i][2];
            twoArray[row][col] = ys;
        }
        return twoArray;
    }

    /**
     * 稀疏数组转二维数组
     *
     */
    public static int[][] sparseArrayToTwoArray() {
        int[][] twoArray = null;
        try {
            File file = new File("/Users/wuzhiqian/idea_repo/siyuan/doc/data.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader reader = new BufferedReader(fileReader);
            String s = null;
            int count = 0;
            while ((s = reader.readLine()) != null){
                if (count == 0){
                    twoArray = new int[row][col];
                }else {
                    String[] sparseArr = s.split(" ");
                    int row = Integer.parseInt(sparseArr[0]);
                    int col = Integer.parseInt(sparseArr[1]);
                    int ys = Integer.parseInt(sparseArr[2]);
                    twoArray[row][col] = ys;
                }
                count++;
            }
            reader.close();
            fileReader.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return twoArray;
    }

    /**
     * 打印数组
     *
     * @param sparseArr 稀疏数组
     */
    public static void showArr(int[][] sparseArr) {
        for (int[] ints : sparseArr) {
            for (int v : ints) {
                System.out.printf("%d\t", v);
            }
            System.out.println();
        }
    }
}
